//
//  GoodsCell.m
//  LoveFreshBeen
//
//  Created by Cyan on 2016/10/16.
//  Copyright © 2016年 魏根. All rights reserved.
//

#import "GoodsCell.h"
#import <UIImageView+WebCache.h>
#import "AddShopCarsAnimationTool.h"
#import "BaseTabBarVC.h"
#import "HMHomeFirstModel.h"
#import "ShopCartGoodsListTool.h"
@interface  GoodsCell ()

@property (weak, nonatomic) IBOutlet UIView *coverLine;
@property (weak, nonatomic) IBOutlet UIImageView *icon;// 商品图片
@property (weak, nonatomic) IBOutlet UILabel *name;// 商品名称
@property (weak, nonatomic) IBOutlet UILabel *quantity;// 商品数量
@property (weak, nonatomic) IBOutlet UILabel *now_price;// 当前价格
@property (weak, nonatomic) IBOutlet UILabel *last_price;// 之前价格
@property (weak, nonatomic) IBOutlet UIImageView *one;// 买一赠一

@property (weak, nonatomic) IBOutlet UIButton *lessBtn;

@property (weak, nonatomic) IBOutlet UILabel *numLabel;
@property (weak, nonatomic) IBOutlet UIButton *plusBtn;

@end

@implementation GoodsCell

- (void)awakeFromNib {
    [super awakeFromNib];
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
}

-(void)setModel:(HMHomeFirstModel *)model{
    _model = model;
    self.name.text = model.name;
    self.quantity.text = model.specifics;
    self.now_price.text = [NSString stringWithFormat:@"%.2f",model.partner_price];
    self.last_price.text =[NSString stringWithFormat:@"%.2f",model.market_price];
    self.numLabel.text = [NSString stringWithFormat:@"%ld",self.model.goodsCount];
    self.one.hidden = [model.pm_desc isEqualToString:@"买一赠一"] ? NO : YES;
    [self.icon sd_setImageWithURL:[NSURL URLWithString:model.img]];
        self.coverLine.hidden = [self.last_price.text isEqualToString:self.now_price.text] ? YES : NO;
    self.model.image = self.icon.image;
    
    self.lessBtn.hidden = !model.goodsCount;
    self.numLabel.hidden = !(model.goodsCount > 0);
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    
}


- (IBAction)lessBtnClick:(UIButton *)sender {

    self.model.goodsCount --;

    self.numLabel.text = [NSString stringWithFormat:@"%ld",self.model.goodsCount];
    [[ShopCartGoodsListTool sharedGoodsListTool] reduceGoodsFromShopCartGoodsList:self.model];
    if (self.model.goodsCount == 0) {
        self.lessBtn.hidden = YES;
        self.numLabel.hidden = YES;
    }
                [[NSNotificationCenter defaultCenter] postNotificationName:KChangeGoodCountNoti object:self.model];
}


- (IBAction)plusBtnClick:(UIButton *)sender {
    
    self.model.goodsCount ++;

    if (self.model.goodsCount > 0) {
        self.lessBtn.hidden = NO;
        self.numLabel.hidden = NO;
    }
    self.numLabel.text = [NSString stringWithFormat:@"%ld",self.model.goodsCount];
    [[ShopCartGoodsListTool sharedGoodsListTool] addGoodsToShopCartGoodsList:self.model];
    CGRect irect = [self convertRect:self.icon.frame toView:self.superview];
    
    [[AddShopCarsAnimationTool shareTool] startAnimationWithView:self.icon withRect:irect withEndRectPoint:CGPointMake([UIScreen mainScreen].bounds.size.width/4*2.5, [UIScreen mainScreen].bounds.size.height - 30) withfinishBlock:^(BOOL finish) {
        

        
        BaseTabBarVC *tabBarVc =(BaseTabBarVC *)[UIApplication sharedApplication].keyWindow.rootViewController;
        
        UIView *tabView = tabBarVc.tabBar.subviews[3];
        
        [AddShopCarsAnimationTool scaleAnimation:tabView];
        
        [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
        [SVProgressHUD setMinimumDismissTimeInterval:1.2];
        [SVProgressHUD showSuccessWithStatus:@"添加成功!"];
    }];
    
            [[NSNotificationCenter defaultCenter] postNotificationName:KChangeGoodCountNoti object:self.model];
}




@end
